#import <QTKit/QTKit.h>
#import "ORGSSAppDelegate.h"
#import "../core/openrgss.h"
#import "ORGSSAudioImplementation.h"
#import "ORGSSGraphicsImplementation.h"

static void initialize_orgss_core ( void ) {
	ORGSSGraphicsMList gfxList = {
		ORGSSGraphicsUpdate,
		NULL,
		NULL,
		NULL
	};
	
	ORGSSInputMList inputList = {
	};
	
	ORGSSAudioMList audioList = {
		ORGSSPlayBGM, 
		ORGSSPlayBGS,
		ORGSSPlayME,
		ORGSSPlaySE,
		
		ORGSSStopBGM, 
		ORGSSStopSE
	};
	
	ORGSS_Init(&gfxList, &inputList, &audioList);
}

@implementation ORGSSAppDelegate

@synthesize window;
@synthesize view;

- (void) applicationDidFinishLaunching: (NSNotification *) aNotification {
	//	Creates a new thread, which calls the method -initializeCoreAndRun
	[NSThread detachNewThreadSelector: @selector(initializeCoreAndRun) toTarget: self withObject: nil];
}

//	This method is implemented so that the app will automatically exit after the window is closed.
- (BOOL) applicationShouldTerminateAfterLastWindowClosed: (NSApplication *) app {
	return YES;
}

- (void) initializeCoreAndRun {
	//	Calls [QTMovie enterQTKitOnThread], which allows for QTKit calls
	[QTMovie enterQTKitOnThread];
	
	//	Initialize the ORGSS core; calls a function so that local variables required
	//	for initialization can be cleaned up after ORGSS_Init() is called.
	initialize_orgss_core();
	
	//	Loads the file main.rb and executes Ruby.
	ORGSS_Main();
	
	//	Cleans up after +enterQTKitOnThread
	[QTMovie exitQTKitOnThread];
}

@end
